#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "strlib.h"

// #define __DEBUG__

str getAtt( const char *rowAttStr, const char *Att )
{
  str ret ;
  char *p = strstr(rowAttStr, Att);


  if ( p==NULL ) {
    ret.state = FAILED;
    sprintf(ret.text, "NO Attritube");
  }

  else {
    int hasEqual = 0;
    char *beginPos = 0, *endPos = 0;
    
    p += strlen(Att);
    for (int i=0; p[i]!='\0'; i++) {
      if ( p[i]==' ' )
        continue;
      // 查找等号
      if ( !hasEqual ) {
        if ( p[i]=='=' )
          hasEqual = 1;
        else {
          ret.state = WRONG;
          sprintf(ret.text, "Unexpected charactor '%c'", p[i]);
          return ret;
        }
      }
      // 查找引号
      else if ( p[i]=='\'' ) {
        if ( !beginPos )
          beginPos = p+i+1;
        else {
          endPos = p+i;
          break;
        }
      }
      // 错误项
      else if ( !beginPos) {
        ret.state = WRONG;
        sprintf(ret.text, "Unexpected charactor '%c'", p[i]);
        return ret;
      }
    }

    if ( !endPos ) {
      ret.state = WRONG;
      sprintf(ret.text, "Uncomplete string");
      return ret;
    }

    else {
      ret.state = SUCCESS;
      int i = 0;
      for (char *p=beginPos; p<endPos; p++)
        ret.text[i++] = *p;
      ret.text[i] = '\0';
    }
  }
  return ret;
}


#ifdef __DEBUG__
int main() {
  char *s = " title='名字', color =  '78'    ";

  str att = getAtt(s, "color");

  if ( SUCCESS==att.state ) {
    printf(att.text);
  }

  else {
    printf("什么也没有");
  }
}
#endif